package com.aspsine.multithreaddownload.demo.callbacks;

import com.aspsine.multithreaddownload.CallBack;
import com.aspsine.multithreaddownload.DownloadException;
import com.aspsine.multithreaddownload.App;
import com.aspsine.multithreaddownload.ResourceTable;
import com.aspsine.multithreaddownload.demo.entity.AppInfo;
import com.aspsine.multithreaddownload.demo.ui.ListItemProvider;
import com.aspsine.multithreaddownload.demo.util.Utils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import java.io.File;

import java.text.DecimalFormat;

public class DownloadCallback extends AbilitySlice implements CallBack {

    private ListContainer listContainer;

    private ListItemProvider listItemProvider;

    private int mPosition;

    private AppInfo mAppInfo;

    EventHandler handler;

    public DownloadCallback(int position, AppInfo appInfo) {
        this.mPosition = position;
        this.mAppInfo = appInfo;
    }

    public DownloadCallback(int position, AppInfo appInfo, ListContainer cont, ListItemProvider prover) {
        this.mAppInfo = appInfo;
        this.mPosition = position;
        this.listContainer = cont;
        this.listItemProvider = prover;
        handler = new EventHandler(EventRunner.current());
    }

    @Override
    public void onStarted() {

    }

    @Override
    public void onConnecting() {
        mAppInfo.setStatus(AppInfo.STATUS_CONNECTING);
        if (isCurrentListViewItemVisible(mPosition)) {
            Component component = listItemProvider.getComItem(mPosition);
            Text status = (Text) component.findComponentById(ResourceTable.Id_tvStatus);
            status.setText(mAppInfo.getStatusText());
            Button bdown = (Button) component.findComponentById(ResourceTable.Id_btnDownload);
            bdown.setText(mAppInfo.getButtonText());
            listItemProvider.notifyDataChanged();
        }
    }

    @Override
    public void onConnected(long total, boolean isRangeSupport) {
        mAppInfo.setStatus(AppInfo.STATUS_DOWNLOADING);
        if (isCurrentListViewItemVisible(mPosition)) {
            Component component = listItemProvider.getComItem(mPosition);
            Text status = (Text) component.findComponentById(ResourceTable.Id_tvStatus);
            status.setText(mAppInfo.getStatusText());
            Button bdown = (Button) component.findComponentById(ResourceTable.Id_btnDownload);
            bdown.setText(mAppInfo.getButtonText());
            listItemProvider.notifyDataChanged();

        }
    }

    @Override
    public void onProgress(long finished, long total, int progress) {
        String downloadPerSize = getDownloadPerSize(finished, total);
        mAppInfo.setProgress(progress);
        mAppInfo.setDownloadPerSize(downloadPerSize);
        mAppInfo.setStatus(AppInfo.STATUS_DOWNLOADING);
        if (isCurrentListViewItemVisible(mPosition)) {

            Component component = listItemProvider.getComItem(mPosition);
            Text status = (Text) component.findComponentById(ResourceTable.Id_tvStatus);
            status.setText(mAppInfo.getStatusText());
            Button bdown = (Button) component.findComponentById(ResourceTable.Id_btnDownload);
            bdown.setText(mAppInfo.getButtonText());

            Text presize = (Text)component.findComponentById(ResourceTable.Id_tvDownloadPerSize);
            presize.setText(downloadPerSize);

            ProgressBar progressBar = (ProgressBar) component.findComponentById(ResourceTable.Id_progressBar);
            progressBar.setProgressValue(progress);
            listItemProvider.notifyDataChanged();

        }
    }

    @Override
    public void onCompleted() {
        mAppInfo.setStatus(AppInfo.STATUS_COMPLETE);
        File apk = new File(Utils.mDownloadDir, mAppInfo.getName() + ".jpg");
        if (apk.isFile() && apk.exists()) {
            try {
              //  if (isCurrentListViewItemVisible(mPosition)) {
                    Component component = listItemProvider.getComItem(mPosition);
                    Text status = (Text) component.findComponentById(ResourceTable.Id_tvStatus);
                    status.setText(mAppInfo.getStatusText());
                    Button bdown = (Button) component.findComponentById(
                            ResourceTable.Id_btnDownload);
                    bdown.setText(mAppInfo.getButtonText());
                    listItemProvider.notifyDataChanged();

              //  }
            }catch (Exception e){
                e.printStackTrace();
            }

            String packageName = Utils.getApkFilePackage(App.getGlobalContext(), apk);
            mAppInfo.setPackageName(packageName);
//            mAppInfo.setStatus(AppInfo.STATUS_INSTALLED);
//            if (Utils.isAppInstalled(MyApplication.getGlobalContext(), packageName)) {
//                mAppInfo.setStatus(AppInfo.STATUS_INSTALLED);
//            }
        }

    }

    @Override
    public void onDownloadPaused() {
        mAppInfo.setStatus(AppInfo.STATUS_PAUSED);
        if (isCurrentListViewItemVisible(mPosition)) {
            Component component = listItemProvider.getComItem(mPosition);
            Text status = (Text) component.findComponentById(ResourceTable.Id_tvStatus);
            status.setText(mAppInfo.getStatusText());
            Button bdown = (Button) component.findComponentById(ResourceTable.Id_btnDownload);
            bdown.setText(mAppInfo.getButtonText());
            listItemProvider.notifyDataChanged();
        }
    }

    @Override
    public void onDownloadCanceled() {
        mAppInfo.setStatus(AppInfo.STATUS_NOT_DOWNLOAD);
        mAppInfo.setDownloadPerSize("");
        if (isCurrentListViewItemVisible(mPosition)) {
            Component component = listItemProvider.getComItem(mPosition);
            Text status = (Text) component.findComponentById(ResourceTable.Id_tvStatus);
            status.setText(mAppInfo.getStatusText());
            Button bdown = (Button) component.findComponentById(ResourceTable.Id_btnDownload);
            bdown.setText(mAppInfo.getButtonText());

            Text presize = (Text)component.findComponentById(ResourceTable.Id_tvDownloadPerSize);
            presize.setText("");
            listItemProvider.notifyDataChanged();
        }
    }

    @Override
    public void onFailed(DownloadException e) {
        mAppInfo.setStatus(AppInfo.STATUS_DOWNLOAD_ERROR);
        mAppInfo.setDownloadPerSize("");
        if (isCurrentListViewItemVisible(mPosition)) {
            Component component = listItemProvider.getComItem(mPosition);
            Text status = (Text) component.findComponentById(ResourceTable.Id_tvStatus);
            status.setText(mAppInfo.getStatusText());
            Button bdown = (Button) component.findComponentById(ResourceTable.Id_btnDownload);
            bdown.setText(mAppInfo.getButtonText());

            Text presize = (Text)component.findComponentById(ResourceTable.Id_tvDownloadPerSize);
            presize.setText("");
            listItemProvider.notifyDataChanged();
        }
    }

    private boolean isCurrentListViewItemVisible(int position) {
        int first = listContainer.getFirstVisibleItemPosition();
        int last = listContainer.getLastVisibleItemPosition();
        return first <= position && position <= last;
    }


    private static final DecimalFormat DF = new DecimalFormat("0.00");
    private String getDownloadPerSize(long finished,long total){
        return DF.format((float)finished / (1024*1024)) + "M/"
                + DF.format((float) total / (1024 * 1024)) + "M";
    }
}
